package com.xu.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.xu.mapper.CartMapper;
import com.xu.mapper.JournalMapper;
import com.xu.mapper.ProductMapper;
import com.xu.pojo.*;
import com.xu.service.OrderService;
import com.xu.service.ProductService;
import com.xu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Controller
public class MainController {

    @Autowired
    private UserService userService;

    @Autowired
    private ProductService productService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private JournalMapper journalMapper;

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private ProductMapper productMapper;


    @RequestMapping("/toLogin")
    public String toLogin(Model model)
    {
        return "login";
    }

    @RequestMapping("/toHome")
    public String toHome()
    {
        return "index-1";
    }

    @RequestMapping("/login")
    public String login(HttpSession httpSession, int uid, String userpwd, String username, Model model, Journal journal,String code)
    {
        User user = userService.queryUserById(uid);
        if (user==null)
        {
            model.addAttribute("msg2", "用户名不存在");
            return "login";
        }

        httpSession.setAttribute("user", user);
        httpSession.setAttribute("username", user.getUsername());
        httpSession.setAttribute("uid", user.getUid());
        httpSession.setAttribute("userid", user.getUserid());
        if (userpwd.equals(user.getUserpwd()))
        {
            if (VerifyCode.verifyCode.equals(code)){
                if (uid==1)
                {
                    model.addAttribute("msg", user.getUsername());
                    int num1 = productService.selectProductNum();
                    int num2 = orderService.selectOrderNum();
                    int num3 = userService.getAllUser().size();
                    int num4 = journalMapper.todayGet();
                    model.addAttribute("num1", num1);
                    model.addAttribute("num2", num2);
                    model.addAttribute("num3", num3);
                    model.addAttribute("num4", num4);

                    //日志写入
                    Date date = new Date();
                    journal.setDate(date);
                    journal.setId(uid);
                    journal.setAction("登入");
                    journal.setType("管理员");
                    journalMapper.insertJournal(journal);

                    StpUtil.login(uid);

                    return "admin/dashboard";
                }
                else
                {
                    model.addAttribute("msg", user.getUsername());
                    int num1 = productService.selectProductNum();
                    model.addAttribute("num1", num1);
                    String username1 = user.getUsername();
                    String substring = username1.substring(1);
                    model.addAttribute("num2",substring );

                    //日志写入
                    Date date = new Date();
                    journal.setDate(date);
                    journal.setId(uid);
                    journal.setAction("登入");
                    journal.setType("用户");
                    journalMapper.insertJournal(journal);

                    //查询商品
                    List<Product> products = productService.queryAllProduct();
                    model.addAttribute("prducts", products);

                    List<Product> products1 = productMapper.randomSelectProduct();
                    model.addAttribute("products1", products1);

                    //查询购物车
                    List<Cart> carts = cartMapper.queryCartByUid(uid);
                    model.addAttribute("carts", carts);

                    StpUtil.login(uid);

                    return "index-2";
                }
            }else {
                model.addAttribute("msg2", "验证码有误");
                return "login";
            }
        }
        else
        {
            model.addAttribute("msg2", "用户名/密码有误");
            return "login";
        }
    }
    @RequestMapping("/signOut")
    public String signOut(HttpSession session, Model model,Journal journal)
    {
        //转换登录id
        if (session.getAttribute("uid")==null)
        {
            return "index-2";
        }
        String uid = session.getAttribute("uid").toString();
        int uuid = Integer.parseInt(uid);
        //登录日志写入
        Date date = new Date();
        journal.setDate(date);
        journal.setId(uuid);
        journal.setAction("登出");
        if (session.getAttribute("uid").toString()=="1")
        {
            journal.setType("管理员");
        }
        else
        {
            journal.setType("用户");
        }
        journalMapper.insertJournal(journal);

        session.removeAttribute("user");
        session.removeAttribute("username");
        session.removeAttribute("uid");
        session.removeAttribute("userid");
        model.addAttribute("msg3", "已退出登录");

        List<Product> products1 = productMapper.randomSelectProduct();
        model.addAttribute("products1", products1);

        List<Product> products = productService.queryAllProduct();
        model.addAttribute("prducts", products);

        return "index-2";
    }
    @RequestMapping("/toDashboard")
    public String toDashboard(Model model)
    {
        int num1 = productService.selectProductNum();
        int num2 = orderService.selectOrderNum();
        int num3 = userService.getAllUser().size();
        int num4 = journalMapper.todayGet();
        double allprices = 0;
        model.addAttribute("num1", num1);
        model.addAttribute("num2", num2);
        model.addAttribute("num3", num3);
        model.addAttribute("num4", num4);
        return "admin/dashboard";
    }
    @RequestMapping("/toDashboardUser")
    public String toDashboardUser1(Model model)
    {
        int num1 = productService.selectProductNum();
        model.addAttribute("num1", num1);
        return "dashboard-user";
    }

    @RequestMapping("/toDashboardUser2")
    public String toDashboardUser2(Model model)
    {
        List<Product> products = productService.queryAllProduct();
        model.addAttribute("prducts", products);
        List<Product> products1 = productMapper.randomSelectProduct();
        model.addAttribute("products1", products1);

        return "index-2";
    }

    @RequestMapping("/toOrder")
    public String toOrder()
    {
        return "order";
    }
    @RequestMapping("/toProduct")
    public String toProduct()
    {
        return "product";
    }
    @RequestMapping("/toRegister")
    public String toRegister()
    {
        return "user/register-1";
    }
    @RequestMapping("/toMinus")
    public String toMinus()
    {
        return "minus";
    }
    @RequestMapping("/toMinus2")
    public String toMinus2()
    {
        return "minus2";
    }
    @RequestMapping("/minus")
    public String minus(String gun,double rentprice,double price,Model model)
    {
        double rentpriceAll = ((rentprice*18)/price)*100;
        System.out.println("rentpriceAll===========>"+rentpriceAll);
        BigDecimal b = new   BigDecimal(rentpriceAll);
        double f1=b.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
        model.addAttribute("gun", gun);
        model.addAttribute("rentprice", rentprice);
        model.addAttribute("price", price);
        model.addAttribute("rentpriceAll", f1);
        return "minus";
    }

}
